package com.kingdee.eas.custom.buscost.handler;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.buscostchange.BusCostChangeFactory;
import com.kingdee.eas.custom.buscostchange.BusCostChangeInfo;
import com.kingdee.eas.custom.buscostchange.IBusCostChange;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.EditHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;

/**
 * 
 * Title: BusCostHandler Description: 根据姓名带出所在部门
 * 
 * @author saisai_cheng Email:854296216@qq.com
 * @date 2019-7-31
 */
public class BusCostHandler extends EditHandler {

	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.buscost.handler.BusCostHandler");

	private Context ctx;

	public BusCostHandler(Context ctx) {
		this.ctx = ctx;
	}

	public BusCostHandler() {
		this.ctx = SHRContext.getInstance().getContext();
	}

	/**
	 * 
	 * <p>
	 * Title: getDeptAction
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @throws SHRWebException
	 */
	// 选择姓名，带出部门
	@SuppressWarnings("unchecked")
	public void getDeptAction(HttpServletRequest request,
			HttpServletResponse response, ModelMap model)
			throws SHRWebException {

		String id = request.getParameter("id");
		// 部门
		String dept = null;

		Map map = new HashMap();

		StringBuffer sql = new StringBuffer();

		sql
				.append("SELECT org.fname_l2 as orgName FROM T_BD_Person person left join T_ORG_ADMIN  org on org.fid=person.fhrorgunitid where person.fid = ");
		sql.append("'" + id + "'");

		System.out.println("============sql:" + sql.toString() + "===========");

		IRowSet rowSet = null;

		try {
			rowSet = DbUtil.executeQuery(ctx, sql.toString());
			while (rowSet.next()) {
				dept = rowSet.getString("orgName");
				if (dept != null && !"".equals(dept)) {
					map.put("dept", dept);
				}
			}

		} catch (BOSException e) {
			logger.error("系统有误，请和管理员联系...");
		} catch (SQLException e) {
			logger.error("系统有误，请和管理员联系...");
		}
		JSONUtils.SUCCESS(map);
	}

	/**
	 * 
	 * <p>
	 * Title: submitEffectAction
	 * </p>
	 * <p>
	 * Description:提交生效
	 * </p>
	 * 
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws SHRWebException
	 */
	public void submitEffectAction(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException {
		BusCostChangeInfo info = (BusCostChangeInfo) request
				.getAttribute("dynamic_model");

		/*
		 * if (info.getParent().getId() != null) { // 单据先保存，再提交生效 String uuid =
		 * info.getParent().getId().toString(); System.out.println("获取uuid");
		 * updateBillState(uuid);
		 * 
		 * } else { super.saveAction(request, response, modelMap);
		 * super.submitAction(request, response, modelMap); }
		 */

		String uuid = null;
		if (info.getId() != null) {
			uuid = info.getString("id");

		}
		if (StringUtils.isEmpty(uuid)) {
			// 直接点击的提交生效 需要先进行保存
			super.saveAction(request, response, modelMap);
			uuid = info.getId().toString();
			// saveName(uuid);
			// saveCustomData(request, new ObjectUuidPK(uuid));
		}
		updateBillState(new ObjectUuidPK(uuid), 3);
	}

	/**
	 * 
	 * <p>
	 * Title: updateBillState
	 * </p>
	 * <p>
	 * Description:更新单据状态
	 * </p>
	 * 
	 * @param id
	 */
	private void updateBillState(String id) {
		String sql = " update CT_BUS_BusCostChange set fbillstate = '3' where fid = '"
				+ id + "'";
		try {
			DbUtil.execute(ctx, sql);
		} catch (BOSException e) {
			e.printStackTrace();
			logger.error("更新单据状态失败！" + e);
		}
	}

	/**
	 * 
	 * <p>
	 * Title: updateBillState
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 更新单据状态
	 * 
	 * @param pk
	 * @param state
	 *            0 未提交 saved；1 未审批submited；2 审批中auditing；3 审批通过audited；4
	 *            审批未通过auditend
	 * @throws SHRWebException
	 */
	private void updateBillState(IObjectPK pk, Integer state)
			throws SHRWebException {
		IBusCostChange instance;
		try {
			instance = BusCostChangeFactory.getRemoteInstance();
			BusCostChangeInfo applicationBillInfo = instance
					.getBusCostChangeInfo(pk);
			switch (state) {

			case 0: // 未提交 saved

				applicationBillInfo.setBillState(HRBillStateEnum.SAVED);

				break;

			case 1: // 未审批 submited

				applicationBillInfo.setBillState(HRBillStateEnum.SUBMITED);

				break;
			case 2: // 审批中 auditing

				applicationBillInfo.setBillState(HRBillStateEnum.AUDITING);

				break;
			case 3: // 审批通过 audited

				applicationBillInfo.setBillState(HRBillStateEnum.AUDITED);

				break;
			case 4: // 审批未通过 auditend

				applicationBillInfo.setBillState(HRBillStateEnum.AUDITEND);

				break;

			default:

				applicationBillInfo.setBillState(HRBillStateEnum.SAVED);

				break;

			}
			instance.save(applicationBillInfo);

		} catch (EASBizException e) {
			e.printStackTrace();
		} catch (BOSException e) {
			e.printStackTrace();
		}
	}
}
